package com.cxg.fairy.ec.main.personal.order;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.util.log.LatteLogger;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.ec.main.personal.PersonalDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;

import java.util.List;

import butterknife.BindView;

/**
 * Created by CFairy
 */

public class OrderListDelegate extends LatteDelegate implements ISuccess{

    private int mType = 0;

    private static OrderListEntryDelegate mDelegate = null;

    public static final String ORDER_TYPE = "ORDER_TYPE";

    @BindView(R2.id.rv_order_list)
    RecyclerView mRecyclerView = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_order_list;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        mType = args.getInt(ORDER_TYPE);
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        initData();
    }

    public static OrderListDelegate create(int orderType,OrderListEntryDelegate orderListEntryDelegate) {
        final Bundle args = new Bundle();
        args.putInt(ORDER_TYPE, orderType);
        mDelegate = orderListEntryDelegate;
        OrderListDelegate delegate = new OrderListDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    private void initData() {
        RestClient.builder()
                .loader(getContext())
                .url("order/getOrderByOrderType")
                .params("orderType", mType)
                .params("uuid", AccountManager.getSignUUID())
                .success(this)
                .build()
                .post();
    }

    @Override
    public void onSuccess(String response) {
                final LinearLayoutManager manager = new LinearLayoutManager(getContext());
                if (mRecyclerView != null) {
                    mRecyclerView.setLayoutManager(manager);
                    final List<MultipleItemEntity> data = new OrderListDataConverter().setJsonData(response).convert();
                    final OrderListAdapter adapter = new OrderListAdapter(data);
                    adapter.setmDelegate(mDelegate);
                    mRecyclerView.setAdapter(adapter);
                } else {

                }
    }

}
